package com.reny.ll.git.mvvm.extras

import android.content.Context
import android.os.Build
import android.view.View
import android.view.ViewGroup
import com.reny.ll.git.mvvm.R

private fun getStatusBarHeight(context: Context): Int {
    var mStatusBarHeight = 0
    if (0 == mStatusBarHeight) {
        val resourceId: Int =
            context.resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            mStatusBarHeight = context.resources.getDimensionPixelSize(resourceId)
        }
    }
    return mStatusBarHeight
}

fun Any.setStatusHeight(status: View){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        try {
            status.post {
                //父类不是ViewGroup类型的会报错
                val lp: ViewGroup.LayoutParams = status.layoutParams
                lp.height = getRDimenPx(R.dimen.status_bar_height)
                val statusHeight = getStatusBarHeight(status.context)
                //loge("statusHeight=$statusHeight")
                if (statusHeight > 0) {
                    lp.height = statusHeight
                }
                //showToast("status_bar_height:${lp.height}")
                status.layoutParams = lp
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }
}
